package org.algorithm.common;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int x) {
        val = x;
    }

    public ListNode() {
    }

    /**
     *  查看链表
     * @param node
     */
    public static void print(ListNode node) {
        ListNode tmp = node;
        while (tmp != null) {
            System.out.print(tmp.val);
            tmp = tmp.next;
            if (tmp != null) {
                System.out.print(" -> ");
            }
        }
    }


    /**
     *  构建链表
     * @param nums
     * @return
     */
    public static ListNode build(int... nums){
        ListNode root = new ListNode();
        ListNode dummy = root;
        for (int num: nums) {
            ListNode node = new ListNode(num);
            dummy.next = node;
            dummy = node;
        }
        return root.next;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }
}
